package com.cwf.business.activity;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.cwf.business.MyApplication;
import com.cwf.business.R;
import com.cwf.business.activity.fragment.CentralityFragment;
import com.cwf.business.activity.fragment.HomeFragment;
import com.cwf.business.activity.fragment.ManageFragment;
import com.cwf.business.util.MainUtil;

public class MainActivity extends FragmentActivity implements View.OnClickListener {

    public static final String LOGOUT_KEY_NAME = "LOGOUT_KEY_NAME";
    private FrameLayout contentFrame;

    private int selectTab = 0;
    private RelativeLayout homePressed;
    private ImageView homeImg;
    private TextView homeTextView;

    private RelativeLayout managePressed;
    private ImageView manageImg;
    private TextView manageTextView;

    private RelativeLayout centralityPressed;
    private ImageView centralityImg;
    private TextView centralityTextView;

    //首页
    private HomeFragment homePage;
    //管理
    private ManageFragment managePage;
    //中心
    private CentralityFragment centralityPage;


    private MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        MainUtil.setStatusBarColor(this, getResources().getColor(R.color.colorPrimary));
        AppActivityManager.getAppActivityManager().addActivity(this);
        overridePendingTransition(android.R.anim.slide_in_left,
                android.R.anim.slide_out_right);
        setContentView(R.layout.activity_main);

        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE
                | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

        app = (MyApplication) getApplication();

        initView();

        //选择第一个
        setTabSelection(0);
        //initView();
    }

    private void initView() {
        contentFrame = (FrameLayout) findViewById(R.id.content_frame);

        homePressed = (RelativeLayout) findViewById(R.id.home_pressed);
        homeImg = (ImageView) findViewById(R.id.home_pressed_imageView);
        homeTextView = (TextView) findViewById(R.id.home_pressed_textview);

        managePressed = (RelativeLayout) findViewById(R.id.manage_pressed);
        manageImg = (ImageView) findViewById(R.id.manage_pressed_imageView);
        manageTextView = (TextView) findViewById(R.id.manage_pressed_textview);

        centralityPressed = (RelativeLayout) findViewById(R.id.centrality_pressed);
        centralityImg = (ImageView) findViewById(R.id.centrality_pressed_imageButton);
        centralityTextView = (TextView) findViewById(R.id.centrality_pressed_textview);

        homePressed.setOnClickListener(this);
        managePressed.setOnClickListener(this);
        centralityPressed.setOnClickListener(this);

    }

    private void setTabSelection(int tabSelection) {
        cleanSelection();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        higeFragment(transaction);

        switch (tabSelection) {
            case 0:
                homeImg.setImageResource(R.drawable.nav_home_selector);
                homeTextView.setTextColor(getResources().getColor(R.color.unify_color));
                if (homePage == null) {
                    homePage = new HomeFragment();
                    transaction.add(R.id.content_frame, homePage);
                } else {
                    transaction.show(homePage);
                }
                break;
            case 1:
                manageImg.setImageResource(R.drawable.nav_manage_selector);
                manageTextView.setTextColor(getResources().getColor(R.color.unify_color));
                if (managePage == null) {
                    managePage = new ManageFragment();
                    transaction.add(R.id.content_frame, managePage);
                } else {
                    transaction.show(managePage);
                }
                break;
            /*case 3:
                mMessagePressedImg.setImageResource(R.drawable.iv_message_pressed);
                mIvPressedTextview.setTextColor(getResources().getColor(R.color.tab_orange));
                if (mAliIMFragment == null) {
                    mAliIMFragment = new AliIMFragment();
                    transaction.add(R.id.framelayout, mAliIMFragment);
                } else {
                    transaction.show(mAliIMFragment);
                }
                break;*/
            case 2:
                centralityImg.setImageResource(R.drawable.nav_centrality_selector);
                centralityTextView.setTextColor(getResources().getColor(R.color.unify_color));
                if (centralityPage == null) {
                    centralityPage = new CentralityFragment();
                    transaction.add(R.id.content_frame, centralityPage);
                } else {
                    transaction.show(centralityPage);
                }
                break;
        }

        transaction.commitAllowingStateLoss();
    }

    /**
     * 清除掉所有的选中
     */
    private void cleanSelection() {
        homeImg.setImageResource(R.drawable.nav_home_normal);
        manageImg.setImageResource(R.drawable.nav_manage_normal);
        centralityImg.setImageResource(R.drawable.nav_centrality_normal);

        homeTextView.setTextColor(getResources().getColor(R.color.view_gray));
        manageTextView.setTextColor(getResources().getColor(R.color.view_gray));
        centralityTextView.setTextColor(getResources().getColor(R.color.view_gray));
    }

    private void higeFragment(FragmentTransaction transaction) {

        if (homePage != null) {
            transaction.hide(homePage);
        }
        if (managePage != null) {
            transaction.hide(managePage);
        }
        if (centralityPage != null) {
            transaction.hide(centralityPage);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.home_pressed:
                setTabSelection(0);
                selectTab = 0;
                break;
            case R.id.manage_pressed:
                setTabSelection(1);
                selectTab = 1;
                break;
            case R.id.centrality_pressed:
                setTabSelection(2);
                selectTab = 2;
                break;
        }
    }

}
